﻿using System.Windows;
using System.Windows.Threading;
using YAWD.Utils;
using System;
using System.Threading;
using System.Collections.ObjectModel;

namespace YAWD
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private Mutex _mutex;
        private ResourceDictionary _loadedSkinResourceDictionary;
        private ResourceDictionary _loadedLanguageResourceDictionary;

        private Collection<ResourceDictionary> MergedDictionaries
        {
            get { return Resources.MergedDictionaries; }
        }
        public ResourceDictionary LoadedLanguageResourceDictionary
        {
            get { return _loadedLanguageResourceDictionary; }
        }
        protected override void OnStartup(StartupEventArgs e)
        {
            try
            {
                bool isNewInstance = false;
                _mutex = new Mutex(true, "YAWD.Singleton", out isNewInstance);
                if (!isNewInstance)
                    Current.Shutdown();

                base.OnStartup(e);
            }
            catch (Exception ex)
            {
                CatchException(ex);
            }
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            try
            {
                foreach (ResourceDictionary item in this.MergedDictionaries)
                {
                    if (item.Source != null && item.Source.OriginalString.Contains(@"Skins\"))
                        _loadedSkinResourceDictionary = item;

                    if (item.Source != null && item.Source.OriginalString.Contains(@"Languages\"))
                        _loadedLanguageResourceDictionary = item;
                }
            }
            catch (Exception ex)
            {
                CatchException(ex);
            }

        }
        public void ApplySkin(Uri objSkinDictionaryUri)
        {
            if (string.IsNullOrEmpty(objSkinDictionaryUri.OriginalString) == false)
            {
                ResourceDictionary objNewSkinDictionary = (ResourceDictionary)(LoadComponent(objSkinDictionaryUri));

                if (objNewSkinDictionary != null)
                {
                    this.MergedDictionaries.Remove(_loadedSkinResourceDictionary);
                    _loadedSkinResourceDictionary = objNewSkinDictionary;
                    this.MergedDictionaries.Add(objNewSkinDictionary);
                }
            }
        }
        public void ApplyLanguage(Uri objLanguageDictionaryUri)
        {
            if (string.IsNullOrEmpty(objLanguageDictionaryUri.OriginalString) == false)
            {
                ResourceDictionary objNewLanguageDictionary =
                    (ResourceDictionary)(LoadComponent(objLanguageDictionaryUri));

                if (objNewLanguageDictionary != null)
                {
                    this.MergedDictionaries.Remove(_loadedLanguageResourceDictionary);
                    _loadedLanguageResourceDictionary = objNewLanguageDictionary;
                    this.MergedDictionaries.Add(objNewLanguageDictionary);
                }
            }
        }
        private void Application_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            CatchException(e.Exception);
            e.Handled = true;
        }
        private static void CatchException(Exception ex)
        {
            Util.LogException(ex);
            MessageBox.Show(ex.Message);
        }
    }
}
